﻿namespace EasyToolkit.UI
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using EasyToolkit.UI.MvvmBase;
    using Forms = System.Windows.Forms;

    class MetroFormWpfContainerViewModel : ViewModelBase, IDisposable
    {
        #region attributes and properties
        private Forms.Form formContenair;
        private UserControl parent;

        public string Title
        { get; private set; }

        public Object ControlContainerViewModel
        { get; private set; }

        RelayCommand maxRestoreCommand;
        public ICommand MaxRestoreCommand
        { get { return maxRestoreCommand ?? (maxRestoreCommand = new RelayCommand(param => OnMaxRestore())); } }

        private bool isWindowStatusNormal;
        public bool IsWindowStatusNormal
        {
            get { return isWindowStatusNormal; }
            set 
            {
                isWindowStatusNormal = value;
                OnPropertyChanged("IsWindowStatusNormal");
            }
        }

        private bool isWindowStatusMax;
        public bool IsWindowStatusMax
        {
            get { return isWindowStatusMax; }
            set
            {
                isWindowStatusMax = value;
                OnPropertyChanged("IsWindowStatusMax");
            }
        }

        private string iconName;
        public string IconName
        {
            get { return iconName; }
            set
            {
                iconName = value;
                if (! string.IsNullOrEmpty(iconName))
                {
                    HasIcon = true;
                    if (parent.Resources.Contains("icon_Filter"))
                    {
                        ResourceDictionary mainIcon = new ResourceDictionary();
                        object icon = parent.Resources["icon_Filter"];
                        parent.Resources["MainIcon"] = icon;
                    }
                }
                else
                {
                    HasIcon = false;
                    parent.Resources["MainIcon"] = null;
                }
                OnPropertyChanged("IconName");
            }
        }

        private bool hasIcon;
        public bool HasIcon
        {
            get { return hasIcon; }
            set
            {
                hasIcon = value;
                OnPropertyChanged("HasIcon");
            }
        }

        private string themeColorName;
        public string ThemeColorName
        {
            get { return themeColorName; }
            set
            {
                themeColorName = string.IsNullOrEmpty(value) ? "Blue" : value;
                Uri themeColorUri = new Uri(string.Format("pack://application:,,,/MahApps.Metro;component/Styles/Accents/{0}.xaml", themeColorName));
                ResourceDictionary colorTheme = new ResourceDictionary();
                colorTheme.Source = themeColorUri;
                parent.Resources.MergedDictionaries.Add(colorTheme);

                OnPropertyChanged("ThemeColorName");
            }
        }
        #endregion

        #region .ctors
        public MetroFormWpfContainerViewModel(Forms.Form formContenair, UserControl parent, string title, string colorName, string iconName, object contentViewModel)
        {
            this.formContenair = formContenair;
            this.parent = parent;
            ThemeColorName = colorName;
            IconName = iconName;
            Title = title;
            ControlContainerViewModel = contentViewModel;

            IsWindowStatusNormal = formContenair.WindowState == Forms.FormWindowState.Maximized;
            IsWindowStatusMax = formContenair.WindowState == Forms.FormWindowState.Normal;
        }
        #endregion

        private void OnMaxRestore()
        {
            if (formContenair.WindowState == Forms.FormWindowState.Maximized)
                formContenair.WindowState = Forms.FormWindowState.Normal;
            else
                formContenair.WindowState = Forms.FormWindowState.Maximized;

            IsWindowStatusNormal = formContenair.WindowState == Forms.FormWindowState.Maximized;
            IsWindowStatusMax = formContenair.WindowState == Forms.FormWindowState.Normal;
        }

        public void Dispose()
        {
            if (ControlContainerViewModel is IDisposable)
                (ControlContainerViewModel as IDisposable).Dispose();
        }
    }
}
